package com.shanzhu.hospital.utils;


import okhttp3.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class ApiClient {
    private static final String DEEPSEEK_URL = "https://api.deepseek.com/chat/completions";

    public static String getAIResponse(String prompt, String apiKey) throws IOException {

        //OkHttpClient client = new OkHttpClient();

        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(100, TimeUnit.SECONDS) // 设置连接超时时间
                .readTimeout(300, TimeUnit.SECONDS)    // 设置读取超时时间
                .writeTimeout(150, TimeUnit.SECONDS)   // 设置写入超时时间
                .build();

        MediaType mediaType = MediaType.parse("application/json");
        String jsonBody = String.format("{\"model\":\"deepseek-chat\",\"messages\":[{\"role\":" +
                        "\"user\",\"content\":\"%s\"}],\"stream\":false}",
                prompt.replace("\"", "\\\""));
        //{"model":"deepseek-chat","messages":[{"role":"user","content":"111"}],"stream":false}
        Request request = new Request.Builder()
                .url(DEEPSEEK_URL)
                .post(RequestBody.create(mediaType, jsonBody))
                .addHeader("Authorization", "Bearer " + apiKey)
                .build();

        try (Response response = client.newCall(request).execute()) {

            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
            return response.body().string();
        }
    }
}
